//
//  SISSContactSearchView.m
//  SISSAPP
//
//  Created by 优房网 on 2017/6/8.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSContactSearchView.h"

@interface SISSContactSearchView ()

/**
 textField
 */
@property (weak, nonatomic) UITextField *textField;

@end
@implementation SISSContactSearchView


- (void)setPlaceHolder:(NSString *)placeHolder {
    _placeHolder = placeHolder;
    self.textField.placeholder = placeHolder;
}


#pragma mark -给每个cell中的textfield添加事件，只要值改变就调用此函数
-(void)changedTextField:(UITextField *)textField {
    if ([self.delegate respondsToSelector:@selector(textFieldTextDidchangWithTextField:)]) {
        [self.delegate textFieldTextDidchangWithTextField:textField];
    }
}


/**
 UI
 */
-(void)initUI {
    [super initUI];
    
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    bgView.layer.cornerRadius = 4;
    bgView.layer.masksToBounds = YES;
    [self addSubview:bgView];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search"]];
    [bgView addSubview:imageView];
    
    UITextField *textField = [[UITextField alloc] init];
    textField.placeholder = self.placeHolder;
    [textField setValue:COLOR(0xA2AEBA) forKeyPath:@"placeholderLabel.textColor"];
    textField.font = [UIFont systemFontOfSize:13];
    textField.textColor = COLOR(0x334A60);
//    textField.delegate = self;
    [bgView addSubview:textField];
    self.textField = textField;
    [textField addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    
    UIView *bottomLine = [[UIView alloc] init];
    bottomLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self addSubview:bottomLine];
    
    // 自动布局
    CGFloat margin = 8.0;
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(4 * margin);
        make.top.equalTo(self).offset(margin);
        make.right.equalTo(self).offset(-4 * margin);
        make.bottom.equalTo(bottomLine.mas_top).offset(-margin);
    }];
    
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.mas_equalTo(1);
    }];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView).offset(margin);
        make.centerY.equalTo(bgView);
        make.size.mas_equalTo(CGSizeMake(22, 22));
    }];
    
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView.mas_right).offset(margin * 0.5);
        make.centerY.equalTo(imageView);
        make.right.equalTo(bgView);
    }];
    
}

@end
